﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HighDivePrinter.Model;
using HighDivePrinter.Properties;


using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

namespace HighDivePrinter.Model 
{
    /// <summary>
    /// Dataclass that holds data for a dive. Difficulty i automatically calculated inside the constructor
    /// </summary>
    /// 
    [Serializable()]
    public class Dive : IComparable<Dive>
    {
        #region Properties
        /// <summary>
        /// DatabasId
        /// </summary>
        //private int? _DbId;
        //private Diver _Diver;
        private string _DiveCode;
        private double _Difficulty;
        private int _DiveNumber;
        //private int _QueueOrderNumber;
        private double _DiveHeight;
       // public DiveScoreList _DiveScoreList;
        private double? _TotalScore;
        //
      /*  public int? DbId
        {
            get
            {
                return _DbId;
            }
            set
            {
                _DbId = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DbId"));
            }
        }*/
      /*  public Diver Diver
        {
            get
            {
                return _Diver;
            }
            set
            {
                _Diver = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Diver"));
            }
        }*/
        public string DiveCode
        {
            get
            {
                return _DiveCode;
            }
            set
            {
                _DiveCode = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DiveCode"));
            }
        }
        public double Difficulty
        {
            get
            {
                return _Difficulty;
            }
            set
            {
                _Difficulty = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Difficulty"));
            }
        }
        public int DiveNumber
        {
            get
            {
                return _DiveNumber;
            }
            set
            {
                _DiveNumber = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DiveNumber"));
            }
        }
       /* public int QueueOrderNumber
        {
            get
            {
                return _QueueOrderNumber;
            }
            set
            {
                _QueueOrderNumber = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("QueueOrderNumber"));
            }
        }*/
        public double DiveHeight
        {
            get
            {
                return _DiveHeight;
            }
            set
            {
                _DiveHeight = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("DiveHeight"));
            }
        }
       /* public DiveScoreList DiveScoreList
        {
            get
            {
                 return _DiveScoreList;
            }
            set
            {
                _DiveScoreList=  value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("JudgeScore"));
            }
        }*/
        public double? TotalScore
        {
            get
            {
                return _TotalScore;
            }
            set
            {
                _TotalScore = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("TotalScore"));
            }
        }

        #endregion

        #region Public Methods

        public void CalculateDifficulty()
        {
            if (DiveCode != null && DiveHeight != null)
            {
                Difficulty = 5;//DiveCodeDifficultyCalculator.Instance.GetDifficulty(DiveCode, DiveHeight);
            }
            else
            {
               /* Log.Error("DiveCode = " + DiveCode + " DiveHeight = " + DiveHeight);
                throw new DiveCodeException("DiveCode and DiveHeight cannot be null!");*/
            }
        }

        /// <summary>
        /// Calculates the total score of a dive with a full judge score.
        /// </summary>
    /*    public void CalculateTotalScore()
        {
            TotalScore = (DiveScoreList.Sum() - DiveScoreList.Max() - DiveScoreList.Min()) / (DiveScoreList.Count() - 2);
            TotalScore *= 3 * Difficulty;
        }

        */
       /* public override string ToString()
        {
            string diveString = Diver.FirstName + " " + Diver.LastName + " | Start Number: " + Diver.StartNumber + " | Dive: " +
                                DiveNumber + " | " + DiveCode + " | Height: " + DiveHeight + "m | DD: " + Difficulty;
            if (DiveScoreList.Count > 0)
            {
                diveString += " | Score: ";
                diveString += DiveScoreList[0].Score;
                for (int i = 1; i < DiveScoreList.Count; i++)
                {
                    diveString += " | " + DiveScoreList[i].Score;
                }
            }
            if (TotalScore != null)
                diveString += " | Total: " + TotalScore;
           // if (Date != null)
             //   if (Date.Length > 0)
              //      diveString += " | " + Date.Substring(11, 4).Insert(2, ":");
            return diveString;
        }
        */
        #endregion
    
        #region Constructors
        public Dive() { }

        /// <summary>Creates a new Dive</summary>
        /// <param name="diver">The person that performs the dive</param>
        /// <param name="diveCode">The divecode for the dive</param>
        /// <param name="diveNumber">The dive number</param>
        /// <param name="diveHeight">The height the dive is performed from</param>
        public Dive(Diver diver, string diveCode, int diveNumber, int diveHeight)
        {
           // this.Diver = diver;
            this.DiveCode = diveCode;
            this.DiveNumber = diveNumber;
            this.DiveHeight = diveHeight;
            if (this.Difficulty == 0)
                this.Difficulty = 5;//DiveCodeDifficultyCalculator.Instance.GetDifficulty(diveCode, diveHeight);
            //this.DbId = null;
           // this.DiveScoreList = new DiveScoreList();
            this.TotalScore = null;
        }

        #endregion

        #region IComparable Members

        /// <summary>
        /// Method that defines comparison of two Divers.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(Dive obj)
        {
            int c = this.DiveCode.CompareTo(obj.DiveCode);
            if (c == 0)
                return this.DiveNumber.CompareTo(obj.DiveNumber);
            else
                return c;
        }

        #endregion

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Event declaration.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
